package JZ26_二叉搜索树与双向链表;

class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}

public class Solution {
    private TreeNode head;
    private TreeNode last;

    public TreeNode Convert(TreeNode pRootOfTree) {
        if (pRootOfTree == null){
            return null;
        }

        head = null;
        last = null;

        inorder(pRootOfTree);

        return head;
    }

    private void inorder(TreeNode node) {
        if (node != null){
            inorder(node.left);

            // 进行尾插
            if (last != null){
                last.right = node;
            }else {
                head = node;
            }
            node.left = last;
            last = node;

        }
    }
}
